package com.yunwaikeji.gy_tool_library.mvvm.view.admin;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.lifecycle.Observer;

import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.header.FalsifyFooter;
import com.scwang.smart.refresh.header.FalsifyHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYLoadingInfo;
import com.yunwaikeji.gy_tool_library.info.GYSmartRefreshInfo;
import com.yunwaikeji.gy_tool_library.mvvm.info.GYViewAdminInfo;
import com.yunwaikeji.gy_tool_library.mvvm.net.GYStateModel;
import com.yunwaikeji.gy_tool_library.mvvm.net.GYStateType;
import com.yunwaikeji.gy_tool_library.mvvm.viewmodel.GYViewModel;
import com.yunwaikeji.gy_tool_library.utils.GYUtils;
import com.yunwaikeji.gy_tool_library.utils.GYViewUtils;
import com.yunwaikeji.gy_tool_library.view.GYLinearLayout;
import com.yunwaikeji.gy_tool_library.widget.GYLoadingDialog;
import com.yunwaikeji.gy_tool_library.widget.GYLoadingLayout;
import com.yunwaikeji.gy_tool_library.widget.GYTopBar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYViewAdmin implements GYViewAdminInfo, GYSmartRefreshInfo {
    public static GYViewAdmin newInstance() {
        return new GYViewAdmin();
    }

    private Context context;
    private long initViewDelayed = 300;
    private GYTopBar topBar;
    private GYLoadingLayout loadingLayout;
    private SmartRefreshLayout smartRefreshLayout;
    private GYLinearLayout startLinearLayout;
    private GYLinearLayout endLinearLayout;
    private GYLinearLayout view;
    private View rootView;
    private GYLoadingDialog loadingDialog;
    private final Gson gson = new Gson();
    private boolean statusContentDark;
    private ViewStyle viewStyle = ViewStyle.LOADING_REFRESH;

    public GYViewAdmin create(Context context, int layoutId) {
        return create(LayoutInflater.from(context), layoutId);
    }

    public GYViewAdmin create(LayoutInflater layoutInflater, int layoutId) {
        this.context = layoutInflater.getContext();
        loadingDialog = new GYLoadingDialog(context);
        rootView = layoutInflater.inflate(layoutId, null);
        view = new GYLinearLayout(context)
                .getViewSet()
                .setOrientation(LinearLayout.VERTICAL)
                .toView()
                .getBackgroundBuilder()
                .setBackgroundColor(Color.WHITE)
                .build();
        loadingLayout = new GYLoadingLayout(context);
        loadingLayout.showContent();
        smartRefreshLayout = new SmartRefreshLayout(context);
        setSmartRefresh(HeaderFooter.FALSIFY, HeaderFooter.FALSIFY);
        setSmartRefresh(true, true);
        startLinearLayout = new GYLinearLayout(context)
                .getViewSet()
                .setOrientation(LinearLayout.VERTICAL)
                .toView();
        endLinearLayout = new GYLinearLayout(context)
                .getViewSet()
                .setOrientation(LinearLayout.VERTICAL)
                .toView();
        topBar = new GYTopBar(context);
        return finish();
    }

    public GYViewAdmin finish() {
        if (!view.getViewSet().getChildViews().isEmpty()) {
            view.removeAllViews();
            startLinearLayout.removeAllViews();
            endLinearLayout.removeAllViews();
            smartRefreshLayout.removeAllViews();
        }
        view.addView(topBar);
        view.addView(startLinearLayout);
        if (viewStyle == ViewStyle.NONE || !(rootView instanceof ViewGroup)) {
            view.addView(rootView);
        } else {
            ViewGroup viewGroup = (ViewGroup) rootView;
            List<View> startViewList = new ArrayList<>();
            List<View> centerList = new ArrayList<>();
            List<View> endViewList = new ArrayList<>();
            for (View view : GYViewUtils.getChildViewList(viewGroup)) {
                if (view instanceof GYViewAdminLayout) {
                    GYViewAdminLayout baseViewLayout = (GYViewAdminLayout) view;
                    switch (baseViewLayout.getLayoutLocation()) {
                        case 1://头部
                            startViewList.addAll(baseViewLayout.getViewSet().getViews());
                            break;
                        case 2://尾部
                            endViewList.addAll(baseViewLayout.getViewSet().getViews());
                            break;
                    }
                    baseViewLayout.removeAllViews();
                } else {
                    centerList.add(view);
                }
            }
            viewGroup.removeAllViews();
            startLinearLayout.getViewSet().addViews(startViewList);
            for (View view : centerList) {
                viewGroup.addView(view);
            }
            endLinearLayout.getViewSet().addViews(endViewList);
            if (viewStyle == ViewStyle.LOADING) {
                loadingLayout.setContentView(viewGroup);
                view.addView(loadingLayout);
                GYViewUtils.setSize().setSizePx(loadingLayout, MATCH_PARENT, 0, 1);
            } else if (viewStyle == ViewStyle.REFRESH) {
                smartRefreshLayout.addView(viewGroup);
                view.addView(smartRefreshLayout);
                GYViewUtils.setSize().setSizePx(smartRefreshLayout, MATCH_PARENT, 0, 1);
            } else if (viewStyle == ViewStyle.LOADING_REFRESH) {
                smartRefreshLayout.addView(viewGroup);
                loadingLayout.setContentView(smartRefreshLayout);
                GYViewUtils.setSize().setSizePx(smartRefreshLayout, MATCH_PARENT, MATCH_PARENT);
                view.addView(loadingLayout);
                GYViewUtils.setSize().setSizePx(loadingLayout, MATCH_PARENT, 0, 1);
            }
            GYViewUtils.setSize().setSizePx(viewGroup, MATCH_PARENT, MATCH_PARENT);
        }
        view.addView(endLinearLayout);
        return this;
    }

    public GYViewAdmin setViewStyle(ViewStyle viewStyle) {
        this.viewStyle = viewStyle;
        return this;
    }

    public ViewStyle getViewStyle() {
        return viewStyle;
    }

    public long getInitViewDelayed() {
        return initViewDelayed;
    }

    public GYViewAdmin setInitViewDelayed(long initViewDelayed) {
        this.initViewDelayed = initViewDelayed;
        return this;
    }

    public GYTopBar getTopBar() {
        return topBar;
    }

    public GYViewAdmin setTopBar(GYTopBar topBar) {
        view.removeView(this.topBar);
        this.topBar = topBar;
        view.addView(this.topBar, 0);
        return this;
    }

    public GYLoadingLayout getLoadingLayout() {
        return loadingLayout;
    }

    public SmartRefreshLayout getSmartRefreshLayout() {
        return smartRefreshLayout;
    }

    public View getView() {
        return view;
    }

    public GYLoadingDialog getLoadingDialog() {
        return loadingDialog;
    }

    public GYViewAdmin setSmartRefresh(boolean enableRefresh, boolean enableLoader) {
        smartRefreshLayout.setEnableRefresh(enableRefresh);
        smartRefreshLayout.setEnableLoadMore(enableLoader);
        return this;
    }

    public GYViewAdmin setSmartRefresh(HeaderFooter header, HeaderFooter footer) {
        if (header == HeaderFooter.FALSIFY) {
            smartRefreshLayout.setRefreshHeader(new FalsifyHeader(context));
        } else if (header == HeaderFooter.CLASSICS) {
            smartRefreshLayout.setRefreshHeader(new ClassicsHeader(context));
        }
        if (footer == HeaderFooter.FALSIFY) {
            smartRefreshLayout.setRefreshFooter(new FalsifyFooter(context));
        } else if (header == HeaderFooter.CLASSICS) {
            smartRefreshLayout.setRefreshFooter(new ClassicsFooter(context));
        }
        return this;
    }

    public Gson getGson() {
        return gson;
    }

    public void showToast(String tip) {
        GYUtils.showToastLong(context, tip);
    }

    public void dismissAndFinish() {
        loadingDialog.dismiss();
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadMore();
        }
    }

    public void showError(String tip) {
        if (loadingDialog.isShowing() || smartRefreshLayout.isRefreshing() || smartRefreshLayout.isLoading()) {
            GYUtils.showToastLong(context, tip);
        }
        if (loadingLayout.getShowView() == GYLoadingInfo.ShowView.LOADING) {
            loadingLayout.showError(tip + "\n点击刷新");
        }
    }

    public boolean isStatusContentDark() {
        return statusContentDark;
    }

    public GYViewAdmin setStatusContentDark(boolean statusContentDark) {
        this.statusContentDark = statusContentDark;
        return this;
    }

    public GYViewAdmin showSuccess() {
        dismissAndFinish();
        if (loadingLayout.getShowView() == GYLoadingInfo.ShowView.LOADING) {
            loadingLayout.showContent();
        }
        return this;
    }

    private final Observer<GYStateModel> stateModelObserver = new Observer<GYStateModel>() {
        @Override
        public void onChanged(GYStateModel gyStateModel) {
            if (gyStateModel.getStateType() == GYStateType.LOADING) {

            } else if (gyStateModel.getStateType() == GYStateType.SUCCESS) {
                showSuccess();
            } else if (gyStateModel.getStateType() == GYStateType.EMPTY) {
                showError(gyStateModel.getMsg());
            } else if (gyStateModel.getStateType() == GYStateType.ERROR) {
                showError(gyStateModel.getMsg());
            }
            dismissAndFinish();
        }
    };

    public GYViewAdmin addDataObserver(List<GYViewModel> viewModelList) {
        return addDataObserver(stateModelObserver, viewModelList);
    }

    public GYViewAdmin addDataObserver(Observer<GYStateModel> stateModelObserver, List<GYViewModel> viewModelList) {
        for (GYViewModel viewModel : viewModelList) {
            viewModel.getRequestData().observeForever(stateModelObserver);
            viewModel.getResponseData().observeForever(stateModelObserver);
        }
        return this;
    }

    public GYViewAdmin addDataObserver(Observer<GYStateModel> stateModelObserver, GYViewModel... viewModels) {
        return addDataObserver(stateModelObserver, Arrays.asList(viewModels));
    }

    public GYViewAdmin addDataObserver(GYViewModel... viewModels) {
        return addDataObserver(Arrays.asList(viewModels));
    }

    public GYLinearLayout getStartLinearLayout() {
        return startLinearLayout;
    }

    public GYLinearLayout getEndLinearLayout() {
        return endLinearLayout;
    }

    public View getRootView() {
        return rootView;
    }

    public Context getContext() {
        return context;
    }
}
